<template>
    <van-form @submit="onSubmit">
        <van-cell-group inset>
            <van-field
                v-model="form.account"
                name="account"
                label="用户名"
                placeholder="用户名"
                :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
                v-model="form.password"
                type="password"
                name="password"
                label="密码"
                placeholder="密码"
                :rules="[{ required: true, message: '请填写密码' }]"
            />
        </van-cell-group>
        <div style="margin: 16px">
            <van-button round block type="primary" native-type="submit"> 提交 </van-button>
        </div>
    </van-form>
</template>
<script lang="ts" setup>
import { login, loginInput } from "../api"
import { reactive } from "vue"
import {useRouter} from "vue-router"
import { showNotify } from "vant"
import {storeToRefs} from "pinia";
import {userStore} from "@/store/modules/user"
const userSTate=userStore()
const {userInfo}=storeToRefs(userSTate)
const router = useRouter()
const form = reactive<loginInput>({
    account: "",
    password: ""
})

const onSubmit = async (values: any) => {
    console.log("submit", values)
    let res = await login(form)
    console.log(res)
    if (res.code === 0) {
        showNotify({ type: "primary", message: "登录成功!" })
        localStorage.setItem("token","Bearer "+res.token!)
        localStorage.setItem("userInfo",JSON.stringify(res.body))
        userInfo.value=res.body
        // console.log(userInfo.value,'userInfo.value')
        router.replace("home")
    } else {
        showNotify({ type: "danger", message: res.msg! })
    }
}
</script>
<style lang="less" scoped></style>
